package com.sigmazero13.bgjeeves.utils;

import java.util.List;

import android.content.Context;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.sigmazero13.bgjeeves.model.GamePlayInfo;
import com.sigmazero13.bgjeeves.model.GamePlayerInfo;
import com.sigmazero13.bgjeeves.R;

public class PlayListAdapter extends ArrayAdapter<GamePlayInfo> {
	private Context mContext;
	private boolean showGame;
	
	public PlayListAdapter(Context context, int textViewResourceId, List<GamePlayInfo> plays, boolean showGame) {
		super(context, textViewResourceId, plays);
		mContext = context;
		this.showGame = showGame;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.play_list_item, null, false);
		}
		GamePlayInfo play = getItem(position);
		if (play != null) {
			TextView gameName = (TextView)v.findViewById(R.id.play_list_item_game);
			if (showGame) {
				gameName.setVisibility(View.VISIBLE);
				gameName.setText(play.getGame().getName());
			} else {
				gameName.setVisibility(View.GONE);
			}
			
			TextView datePlayers = (TextView)v.findViewById(R.id.play_list_item_date_players);
			if (datePlayers != null) {
				String dateStr = "Unknown Date";
				if (play.getGameDate() != null)
					dateStr = DateFormat.getDateFormat(mContext).format(play.getGameDate());
				String numPlayersStr = "No Players Recorded";
				if (play.getPlayers() != null && play.getNumPlayers() > 0)
					numPlayersStr = play.getNumPlayers() + " players";
				String text = dateStr + ", " + numPlayersStr;
				Integer length = play.getGameLength();
				if (length != null && length > 0)
					text = text + ", " + length + " mins"; 
				datePlayers.setText(text); 
			}
			
			String location = play.getLocation();
			TextView locView = (TextView)v.findViewById(R.id.play_list_item_location);
			if (location != null && !location.equals("")) {
				locView.setVisibility(View.VISIBLE);
				locView.setText("Location: " + location);
			} else {
				locView.setVisibility(View.GONE);
			}
			
			String scenario = play.getScenario();
			TextView scenView = (TextView)v.findViewById(R.id.play_list_item_scenario);
			if (scenario != null && !scenario.equals("")) {
				scenView.setVisibility(View.VISIBLE);
				scenView.setText("Scenario: " + scenario);
			} else {
				scenView.setVisibility(View.GONE);
			}
			
			String winner = "";
			TextView winnerView = (TextView)v.findViewById(R.id.play_list_item_winner);
			if (play.getPlayers() != null && play.getNumPlayers() > 0) {
				for (GamePlayerInfo player : play.getPlayers()) {
					if (player.getWinner()) {
						String thisWinner = "";
						String playerName = player.getName();
						String playerTeam = player.getCombinedTeams();
						
						if (playerName != null && !playerName.equals(""))
							thisWinner = playerName;
						if (playerTeam != null && !playerTeam.equals("")) {
							if (thisWinner.equals(""))
								thisWinner = playerTeam;
							else 
								thisWinner += " (" + playerTeam + ")";
						}
						
						if (thisWinner.equals(""))
							thisWinner = "Unknown Player";
						
						if (! winner.equals(""))
							winner += ", ";
						
						winner += thisWinner;
					}
				}
				if (winner.equals(""))
					winner = "None";
			}
			if (!winner.equals("")) {
				winnerView.setVisibility(View.VISIBLE);
				winnerView.setText("Winner: " + winner);
			} else {
				winnerView.setVisibility(View.GONE);
			}
		}
		
		return v;	
	}
}
